package com.monkeyboy.tree;

import com.monkeyboy.TreeNode;

/**
 * 题目：
 * 求一个树是不是对称的二叉树
 * <p>
 * 解题思路：
 * 先求出这个二叉树的镜像，然后判断两个是否相等
 *
 * @Author Gavin
 * @date 2021.04.20 11:20
 */
public class Tree06 {
    public boolean solution(TreeNode root) {
        if (root == null) {
            return false;
        }
        TreeNode root2 = root;
        solution2(root2);
        return solution1(root, root2);

    }

    //判断两个二叉树是否相等
    public boolean solution1(TreeNode root1, TreeNode root2) {
        if (root1.val != root2.val) {
            return false;
        }
        return solution1(root1.left, root2.left) && solution1(root1.right, root2.right);
    }


    //求出镜像
    public TreeNode solution2(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode temp = root.right;
        root.right = root.left;
        root.left = temp;
        solution2(root.left);
        solution2(root.right);
        return root;
    }
}
class Solution {

    public boolean jude(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null) {
            return true;
        } else if (node1 == null || node2 == null) {
            return false;
        }

        if (node1.val != node2.val) {
            return false;
        } else {
            return jude(node1.left, node2.right) && jude(node1.right, node2.left);
        }
    }

    public boolean isSymmetrical(TreeNode pRoot) {
        return pRoot==null || jude(pRoot.left, pRoot.right);
    }
}